package org.lq.shopping.service.impl;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.lq.shopping.dao.ShopStockDao;
import org.lq.shopping.dao.impl.ShopStockDaoImpl;
import org.lq.shopping.entity.ShopStock;
import org.lq.shopping.service.ShopStockService;

import com.google.gson.Gson;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class ShopStockServiceImpl implements ShopStockService {

	private static ShopStockDao dao = new ShopStockDaoImpl();
	private static Gson gson = new Gson();
	
	@Override
	public boolean addShopStock(ShopStock ss) {
		return dao.add(ss)>0;
	}

	@Override
	public List<ShopStock> findPage(int pageIndex, int pageSize) {
//		List<ShopStock> list = dao.findPage(pageIndex, pageSize);
		Jedis jedis = setJedis();
//		for (ShopStock ss : list) {
//			String json = gson.toJson(ss);
//		jedis.zadd("stockList", ss.getStock_id(),json);
//		}
		Set<String> list1 = jedis.zrangeByLex("stockList", "-","+",(pageIndex-1)*pageSize,pageIndex*pageSize);
		// List<String> list1 = jedis.lrange("sxtockList", (pageIndex-1)*pageSize, pageIndex*pageSize-1);
		List<ShopStock> stock = new ArrayList<>();
		for (String str: list1) {
			ShopStock s = gson.fromJson(str, ShopStock.class);
			stock.add(s);
		}
		return stock;
	}

	@Override
	public boolean updateShopStock(ShopStock ss) {
		return dao.update(ss)>0;
	}

	@Override
	public boolean deleteShopStock(int id) {
		return dao.delete(id)>0;
	}

	@Override
	public List<ShopStock> findPageStockByPersonName(String name, int pageIndex, int pageSize) {
		List<ShopStock> list = dao.getStockByPersonName(name, pageIndex, pageSize);
		Jedis jedis = getStockByName(name);
		Set<String> list1 = jedis.zrangeByLex("PersonList", "-","+",(pageIndex-1)*pageSize,pageIndex*pageSize);
		// List<String> list1 = jedis.lrange("sxtockList", (pageIndex-1)*pageSize, pageIndex*pageSize-1);
		List<ShopStock> stock = new ArrayList<>();
		for (String str: list1) {
			ShopStock s = gson.fromJson(str, ShopStock.class);
			stock.add(s);
		}
		return stock;
	}

	@Override
	public List<ShopStock> findPageStockBetweenDate(Date beginTime, Date endTime, int pageIndex, int pageSize) {
		Jedis jedis = getStockbetweenTime(beginTime, endTime);
		Set<String> list1 = jedis.zrangeByLex("TimeList", "-","+",(pageIndex-1)*pageSize,pageIndex*pageSize);
		// List<String> list1 = jedis.lrange("sxtockList", (pageIndex-1)*pageSize, pageIndex*pageSize-1);
		List<ShopStock> stock = new ArrayList<>();
		for (String str: list1) {
			ShopStock s = gson.fromJson(str, ShopStock.class);
			stock.add(s);
		}
		return stock;	
		}

	private static Jedis getJedis() {
		JedisPoolConfig config = new JedisPoolConfig();
		//控制最大连接数
		config.setMaxTotal(50);
		//设置空闲连接
		config.setMaxIdle(5);
		//默认-1,等待时间,如果等待时间内没有获得连接,之间抛出异常
		config.setMaxWaitMillis(1000*100);
		//在获取连接之前,validate操作,验证  保证得到连接可用
		config.setTestOnBorrow(true);
		//当返回一个连接pool 的时候先进行ping() 判断是否可以连接
		config.setTestOnReturn(true);
		//配置接口密码
		JedisPool pool = new  JedisPool(config,"127.0.0.1",6379,2000);
		Jedis jedis = pool.getResource();
		jedis.select(6);
		return jedis;
		
	}
	
	
	public static  Jedis setJedis() {
		Jedis jedis = getJedis();
		List<ShopStock> list = dao.findAll();
		for (ShopStock ss : list) {
			String json = gson.toJson(ss);
			jedis.zadd("stockList", ss.getStock_id(),json);
		}
		return jedis;
	}
	public static  Jedis getStockByName(String name) {
		Jedis jedis = getJedis();
		List<ShopStock> list = dao.getStockPersonName(name);
		for (ShopStock ss : list) {
			String json = gson.toJson(ss);
			jedis.zadd("PersonList", ss.getStock_id(),json);
		}
		return jedis;
	}
	public static  Jedis getStockbetweenTime(Date beginTime,Date endTime) {
		Jedis jedis = getJedis();
		List<ShopStock> list = dao.getBetweenDate(beginTime, endTime);
		for (ShopStock ss : list) {
			String json = gson.toJson(ss);
			jedis.zadd("TimeList", ss.getStock_id(),json);
		}
		return jedis;
	}
	
}
